package com.study.order.controller;

import com.study.order.dao.OrderListDao;
import com.study.order.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author caoqianhuan
 * @date 2022/8/16
 * @Description:
 */

@RestController
public class Test {
  /*  @Autowired
    private OrderListDao orderListDao;

    private ReentrantLock lock = new ReentrantLock();

    @PostMapping("/a")
    *//**
     * @author caoqianhuan
     * @date 2022/8/16
     * @desc 使用ReentrantLock 解决
     * @return
     *//*
    public void test(Order order) {
        try {
            lock.lock();
            //获取本类中最后一个排序
            Integer maxSort = orderListDao.getClassMaxSort(order.getClassId());
            if (maxSort == null) {
                maxSort = 0;
            }
            //最后一个序号加一
            order.setSort(maxSort+1);
            //插入
            orderListDao.insert(order);
        } finally {
            //释放锁
            lock.unlock();
        }
    }

    @PostMapping("/b")
    public void testIndex(Order order) {
        Integer maxSort = orderListDao.getClassMaxSort(order.getClassId());
        if (maxSort == null) {
            maxSort = 0;
        }
        //最后一个序号加一
        order.setSort(maxSort+1);
        //插入
        orderListDao.insert(order);
    }*/
    @RequestMapping("/a")
    public void testA() {
        System.out.println("成功访问");
    }

    @Value("${oss.logoutUrl}")
    private String logoutUrl;
    @RequestMapping("/logout")
    public void logout(String ticket, HttpServletResponse response) throws IOException {
        System.out.println("logout执行");
        System.out.println(logoutUrl);
        response.sendRedirect(logoutUrl + "?ticket=" + ticket);
    }
    @RequestMapping("/realLogout")
    public Boolean realLogout(HttpSession session) {
        session.removeAttribute("isLogin");
        if ( session.getAttribute("isLogin") != null && (Boolean) session.getAttribute("isLogin") ) {
            return false;
        }
        return true;
    }
}
